<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="套餐名称" prop="comboName">
        <el-input
          v-model="queryParams.comboName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一管理" prop="packageId">
        <el-select
          v-model="queryParams.packageId"
          placeholder="是否统一管理"
          clearable
          @keyup.enter.native="handleQuery">
          <el-option
            v-for="status in dict.type.sys_dishes_shopping"
            :key="status.value"
            :label="status.label"
            :value="status.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜品状态" prop="comboStatus">
        <el-select
          v-model="queryParams.comboStatus"
          placeholder="是否在售"
          clearable
          @keyup.enter.native="handleQuery">
          <el-option
          v-for="status in dict.type.sys_dishes_status"
          :key="status.value"
          :label="status.label"
          :value="status.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dishes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dishes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dishes:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dishes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="comboList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="套餐名称" align="center" prop="comboName" />
      <el-table-column label="套餐图片" align="center" prop="comboImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.comboImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="套餐内容" align="center" prop="dishesList" width="100">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            width="400"
            trigger="click">
            <el-table :data="scope.row.dishesList" height="350">
              <el-table-column width="100" property="dishesName" label="菜品名称"/>
              <el-table-column width="100" property="dishesImage" label="菜品图片">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.dishesImage" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column width="100" property="dishesPrice" label="价格"/>
              <el-table-column width="100" property="dishesQuantity" label="菜品数量"/>
            </el-table>
            <el-button slot="reference" type="primary" size="small">查看菜品</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="套餐分类" align="center" prop="comboTypeName" />
      <el-table-column label="套餐描述" align="center" prop="comboDescription" />
      <el-table-column label="统一管理" align="center" prop="packageId" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.packageId"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="套餐价格" align="center" prop="comboDishesPrice" />
      <el-table-column label="套餐状态" align="center" prop="comboStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.comboStatus === '1'" type="warning" size="small">下架</el-tag>
          <el-tag v-else type="success" size="small">在售</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dishes:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dishes:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="套餐分类" prop="comboId">
          <el-select v-model="form.comboId" placeholder="请选择套餐类型">
            <el-option
              v-for="type in dict.type.sys_combo_type"
              :key="type.value"
              :label="type.label"
              :value="type.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="套餐名称" prop="comboName">
          <el-input v-model="form.comboName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="套餐图片" prop="comboImage">
          <image-upload v-model="form.comboImage">
            <el-image src="form.comboImage"/>
          </image-upload>
        </el-form-item>
        <el-form-item label="菜品选择" prop="dishesList">
          <el-drawer
            title="重置套餐内容"
            :visible.sync="table"
            direction="rtl"
            size="50%"
            append-to-body
            close-on-click-modal
            show-close>
            <el-form-item label="套餐编码">
              <div class="container">
                <el-input id="CODE" v-model="form.comboCode" placeholder="请输入套餐编码"/>
                <el-button @click="okOnCancel">确定</el-button>
              </div>
            </el-form-item>
            <div style="float: left;margin-left:15px;margin-right: 30px">
              <span>菜品选择</span>
              <el-table :data="comboData">
                <el-table-column width="70" property="dishesImage" label="图片">
                  <template slot-scope="scope">
                    <image-preview :src="scope.row.dishesImage" :width="50" :height="50"/>
                  </template>
                </el-table-column>
                <el-table-column width="70" property="dishesName" label="名称"/>
                <el-table-column width="70" property="dishesPrice" label="价格"/>
                <el-table-column width="160" label="数量" align="center">
                  <template slot-scope="scope">
                    <el-input-number size="mini" :disabled="disabled" v-model="scope.row.dishesQuantity" @change="updateComboTest(scope.row)" :min="0" :max="10" label="描述文字"/>
                  </template>
                </el-table-column>
              </el-table>
              <span>
                <pagination
                  small="true"
                  page-sizes="7"
                  v-show="newTotal>0"
                  :total="newTotal"
                  :page.sync="newQueryParams.pageNum"
                  :limit.sync="newQueryParams.pageSize"
                  @pagination="getAllDishes"
                />
              </span>
            </div>
            <div style="float: left;margin-right: 1%">
              <span>主食饮品选择</span>
              <el-table :data="mainAndDrinkData">
                <el-table-column width="70" property="dishesImage" label="图片">
                  <template slot-scope="scope">
                    <image-preview :src="scope.row.dishesImage" :width="50" :height="50"/>
                  </template>
                </el-table-column>
                <el-table-column width="70" property="dishesName" label="名称"/>
                <el-table-column width="70" property="dishesPrice" label="价格"/>
                <el-table-column width="160" label="数量" align="center">
                  <template slot-scope="scope">
                    <el-input-number size="mini" :disabled="disabled" v-model="scope.row.dishesQuantity" @change="updateComboTest(scope.row)" :min="0" :max="10" label="描述文字"/>
                  </template>
                </el-table-column>
              </el-table>
              <span>
                <pagination
                  small="true"
                  page-sizes="7"
                  v-show="newTotalL>0"
                  :total="newTotalL"
                  :page.sync="newQueryParamsL.pageNum"
                  :limit.sync="newQueryParamsL.pageSize"
                  @pagination="getMainAndDrink"
                />
              </span>
            </div>
          </el-drawer>
          <el-popover
            placement="left"
            width="400"
            trigger="click">
            <el-table :data="form.dishesList" height="350">
              <el-table-column width="100" property="dishesName" label="菜品名称"/>
              <el-table-column width="100" property="dishesImage" label="菜品图片">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.dishesImage" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column width="100" property="dishesPrice" label="价格"/>
              <el-table-column width="100" property="dishesQuantity" label="菜品数量"/>
            </el-table>
            <el-button slot="reference" type="primary" size="small">查看菜品</el-button>
          </el-popover>
          <el-button @click=open2 size="small">重置菜品</el-button>
        </el-form-item>
        <el-form-item label="套餐价格" prop="comboDishesPrice">
          <el-input v-model="form.comboDishesPrice" placeholder="请输入套餐价格" />
        </el-form-item>
<!--        <el-form-item label="菜品图片" prop="dishesImage">
          <image-upload v-model="form.dishesImage">
            <el-image src="form.dishesImage"/>
          </image-upload>
        </el-form-item>-->
        <el-form-item label="套餐描述" prop="comboDescription">
          <el-input v-model="form.comboDescription" placeholder="请输入套餐描述" />
        </el-form-item>
        <el-form-item label="套餐状态" prop="comboStatus">
          <el-select  v-model="form.comboStatus" placeholder="请选择套餐状态">
            <el-option
            v-for="status in dict.type.sys_dishes_status"
            :key="status.value"
            :label="status.label"
            :value="status.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  changeComboStatus,
  listCombo, getCombo, listAll, updateCombo, addCombo, delCombos
} from "@/api/system/mainCombo";
import { shoppingChange} from "@/api/system/totalDishes";
import {listAllMainAndDrink} from "@/api/system/onlyCombo";

export default {
  dicts:['sys_combo_type','sys_dishes_status','sys_dishes_shopping'],
  name: "Dishes",
  data() {
    return {
      table: false,
      disabled: false,
      one: [],
      two:[],
      newDishesCode: [],
      CODE: null,
      drawer: false,
      direction: 'rtl',
      // 最上方弹出层
      onOpen: true,
      // 全部菜品信息
      comboData:[],
      newTotal: 0,
      newTotalL: 0,
      newQueryParams: {
        pageNum: 1,
        pageSize: 7,
      },
      newQueryParamsL: {
        pageNum: 1,
        pageSize: 7,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品管理表格数据
      comboList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        comboName: null,
        packageId: null,
        comboTypeName: null,
        dishesImage: null,
        comboStatus: null,
        dishesDescription: null,
        comboDishesPrice: null,
        comboId: null,
        comboDescription: null,
        dishesList: null,
        comboCode: null,
        comboDishesId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        comboName: [
          { required: true, message: "套餐名称不能为空", trigger: "blur" }
        ],
        comboId: [
          { required: true, message: "套餐分类不能为空", trigger: "change" }
        ],
        comboCode: [
          { required: true, message: "套餐编码不能为空", trigger: "blur" }
        ],
        comboStatus: [
          { required: true, message: "套餐状态不能为空", trigger: "change" }
        ],
        comboDescription: [
          { required: true, message: "套餐描述不能为空", trigger: "change" }
        ],
        comboDishesPrice: [
          { required: true, message: "套餐价格不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getAllDishes();
    this.getMainAndDrink();
  },
  methods: {
    /** 查询菜品管理列表 */
    getList() {
      this.loading = true;
      listCombo(this.queryParams).then(response => {
        this.comboList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        comboName: null,
        packageId: null,
        comboTypeName: null,
        dishesImage: null,
        comboStatus: null,
        dishesDescription: null,
        comboPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.comboDishesId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const comboDishesId = row.comboDishesId || this.ids
      getCombo(comboDishesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.comboDishesId != null) {
            updateCombo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCombo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const comboDishesIds = row.comboDishesId || this.ids;
      this.$modal.confirm('是否确认删除菜品管理编号为"' + comboDishesIds + '"的数据项？').then(function () {
        return delCombos(comboDishesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/mainCombo/export', {
        ...this.queryParams
      }, `dishes_${new Date().getTime()}.xlsx`)
    },
    handleStatusChange(row) {
      let text = row.packageId === "0" ? "移入" : "移出";
      this.$modal.confirm('确认要"' + text + '""' + row.comboName + '"套餐吗？').then(function () {
        return changeComboStatus(row.comboDishesId, row.packageId);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.packageId = row.packageId === "0" ? "1" : "0";
      });
    },
    handleChange(row) {
      shoppingChange(row.dishesId, row.shoppingChange);
      this.$message({
        message: '操作成功，请在购物车中查看',
        type: 'success'
      });

    },
    getAllDishes(){
      this.loading = true;
      listAll(this.newQueryParams).then(request=>{
        this.comboData=request.rows;
        this.newTotal=request.total;
        this.loading=false;
      })
    },
    getMainAndDrink(){
      this.loading = true;
      listAllMainAndDrink(this.newQueryParams).then(request=>{
        this.mainAndDrinkData=request.rows;
        this.newTotalL=request.total;
        this.loading=false;
      })
    },
    open2() {
      this.table=true;
      const h = this.$createElement;
      this.$notify({
        title: '提示',
        message:  h('i', { style: 'color: teal'},'套餐编码 例:+1*1+3*1->凉拌豆腐，泡椒凤爪'),
        customClass: 'http-message',
        position: 'top-left'
      });
    },
    okOnCancel(){
      this.form.comboCode=this.form.comboCode.substring(1);
      this.onOpen=false;
    },
    updateComboTest(row){
      if (this.newDishesCode.includes(row.dishesId+'*')){
        console.log(this.newDishesCode+"开始修改菜品数量");
        this.one = this.newDishesCode.split("+");
        console.log(this.one+"one的初始化赋值");
        console.log(this.one.length+"one的长度");
        for (let i=0;i<this.one.length;i++){
          console.log(this.one[i]+"one【"+i+"】的值");
          if (this.one[i].includes(row.dishesId+'*')){
            this.one[i]=row.dishesId+'*'+row.dishesQuantity;
          }
        }
        console.log(this.one+"修改后one的值");
        this.two = this.one.join("+");
        this.newDishesCode=this.two;
        if (this.newDishesCode.length>40){
          this.disabled=true;
          this.$message.error('你最多可以选择10个菜品/主食哦~确定或重新输入吧');
        }
        console.log(this.newDishesCode+"完成修改菜品数量");
        this.form.comboCode=this.newDishesCode;
      }
      else if (!this.newDishesCode.includes(row.dishesId+'*')){
        let onceCode = row.dishesId + '*' + row.dishesQuantity;
        this.newDishesCode += ("+" + onceCode);
        console.log(this.newDishesCode+"初始化菜品");
        this.form.comboCode=this.newDishesCode;
        if (this.newDishesCode.length>40){
          this.disabled=true;
          this.$message.error('你最多可以选择10个菜品/主食哦~确认或重新输入吧');
        }
      }
    },
  }
};
</script>
<style>
.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.http-message {
  z-index: 9000 !important;
}
</style>
